﻿// ThreadOperation.cpp: 实现文件
//

#include "stdafx.h"
#include "MesTest.h"
#include "ThreadOperation.h"
#include "MesTestDlg.h"
#include "MesGSoap.h"
#include "CDlgStopReason.h"


// ThreadOperation

IMPLEMENT_DYNAMIC(ThreadOperation, CWnd)

ThreadOperation::ThreadOperation()
{

}

ThreadOperation::~ThreadOperation()
{
}


BEGIN_MESSAGE_MAP(ThreadOperation, CWnd)
END_MESSAGE_MAP()



// ThreadOperation 消息处理程序

void ThreadEx::ThreadSendMES(PVOID pVoid)
{
	Sleep(2000);
	__int64	timeStamp = 0;
	while (true)
	{
		bool bFlag = false;	
		if (g_pPlc == NULL)
		{
			return;
		}
		g_pPlc->DoSomething("GETCONNECTION", NULL, &bFlag);
		if (bFlag &&g_nSendMes )
		{			
			static int EquStatius = 0;			
			g_pPlc->DoSomething("READINT32", &g_PLCAddress.EquStatus, &g_SendMes.nEquStatus);
			if (EquStatius != g_SendMes.nEquStatus )  //设备状态采集
			{
				EquStatius = g_SendMes.nEquStatus;
				RECIPEINFO cstrSendData; //MES数据
				cstrSendData.EquipmentCode = g_InfoMes.EquipmentCode;
				cstrSendData.FactoryCode = g_InfoMes.FactoryCode;
				cstrSendData.TestCode = g_InfoMes.TestCode;
				cstrSendData.EquStatus = g_SendMes.nEquStatus;
				cstrSendData.StopReason =999;
				cstrSendData.nType = 2;
				cstrSendData.Port = g_InfoMes.Port;
				cstrSendData.IP = g_InfoMes.IP;
				((CMesTestDlg*)g_pMainDialog)->EquipStatusCollect(cstrSendData);
				//if (EquStatius != 1)
				//{
				//	SYSTEMTIME sysTime;
				//	::GetLocalTime(&sysTime);
				//	CTime curTime(sysTime);
				//	timeStamp = curTime.GetTime();
				//}

				//if (EquStatius == 1 && timeStamp!=0)
				//{
				//	SYSTEMTIME sysTime;
				//	::GetLocalTime(&sysTime);
				//	CTime curTime(sysTime);
				//	__int64 EndtimeStamp = curTime.GetTime();
				//	if (EndtimeStamp - timeStamp > 180)
				//	{
				//		CDlgStopReason Dlg;
				//		Dlg.DoModal();
				//		EquStatius = g_SendMes.nEquStatus;
				//		RECIPEINFO cstrSendData; //MES数据
				//		cstrSendData.EquipmentCode = g_InfoMes.EquipmentCode;
				//		cstrSendData.FactoryCode = g_InfoMes.FactoryCode;
				//		cstrSendData.TestCode = g_InfoMes.TestCode;
				//		cstrSendData.EquStatus = g_SendMes.nEquStatus;
				//		cstrSendData.StopReason = g_SendMes.nStopReason;
				//		cstrSendData.nType = 2;
				//		cstrSendData.Port = g_InfoMes.Port;
				//		cstrSendData.IP = g_InfoMes.IP;
				//		((CMesTestDlg*)g_pMainDialog)->EquipStatusCollect(cstrSendData);

				//	}
				//}
			}

			static int AlarmStatius = 1000;
			if (g_SendMes.nEquStatus == 5 && AlarmStatius!=5)   //发送报警
			{
				g_pPlc->DoSomething("READINT32", &g_PLCAddress.AlarmCode, &g_SendMes.nAlarmCode);
				AlarmStatius = g_SendMes.nEquStatus;
				RECIPEINFO cstrSendData; //MES数据
				cstrSendData.EquipmentCode = g_InfoMes.EquipmentCode;
				cstrSendData.FactoryCode = g_InfoMes.FactoryCode;
				cstrSendData.TestCode = g_InfoMes.TestCode;				
				cstrSendData.AlarmStatius = 1;
				cstrSendData.AlarmCode = g_SendMes.nAlarmCode;
				cstrSendData.nType = 3;
				cstrSendData.Port = g_InfoMes.Port;
				cstrSendData.IP = g_InfoMes.IP;
				((CMesTestDlg*)g_pMainDialog)->EquipStatusCollect(cstrSendData);

			}

			if (g_SendMes.nEquStatus != 5 && AlarmStatius == 5)  //解除报警
			{
				AlarmStatius = 1000;
				RECIPEINFO cstrSendData; //MES数据
				cstrSendData.EquipmentCode = g_InfoMes.EquipmentCode;
				cstrSendData.FactoryCode = g_InfoMes.FactoryCode;
				cstrSendData.TestCode = g_InfoMes.TestCode;
				cstrSendData.AlarmStatius = 0;
				cstrSendData.AlarmCode = g_SendMes.nAlarmCode;
				cstrSendData.nType = 3;
				cstrSendData.Port = g_InfoMes.Port;
				cstrSendData.IP = g_InfoMes.IP;
				((CMesTestDlg*)g_pMainDialog)->EquipStatusCollect(cstrSendData);
			}
			
			g_pPlc->DoSomething("READINT32", &g_PLCAddress.StopReason, &g_SendMes.nStopReason);
			if (g_SendMes.nStopReason !=100)
			{
				g_pPlc->DoSomething("READINT32", &g_PLCAddress.EquStatus, &g_SendMes.nEquStatus);
				EquStatius = g_SendMes.nEquStatus;
				RECIPEINFO cstrSendData; //MES数据
				cstrSendData.EquipmentCode = g_InfoMes.EquipmentCode;
				cstrSendData.FactoryCode = g_InfoMes.FactoryCode;
				cstrSendData.TestCode = g_InfoMes.TestCode;
				cstrSendData.EquStatus = g_SendMes.nEquStatus;
				cstrSendData.StopReason = g_SendMes.nStopReason;
				cstrSendData.nType = 2;
				cstrSendData.Port = g_InfoMes.Port;
				cstrSendData.IP = g_InfoMes.IP;
				((CMesTestDlg*)g_pMainDialog)->EquipStatusCollect(cstrSendData);
				int flag = 100;
				g_pPlc->DoSomething("WRITEINT32", &g_PLCAddress.StopReason, &flag);
			}
		}
		Sleep(200);
	}
	
}
void ThreadEx::ThreadRefreshStatus(PVOID pVoid)
{
	Sleep(2000);
	while (true)
	{
		if (g_nSendMes)
		{
			if (g_pPlc == NULL)
			{
				return;
			}
			bool bFlag;
			g_pPlc->DoSomething("GETCONNECTION", NULL, &bFlag);
			if (!bFlag)
			{
				Sleep(10000);
				continue;
			}
			g_pPlc->DoSomething("READSTRING", &g_PLCAddress.MaterialNum, &g_SendMes.cstrMaterialNum);   //   极卷号
			g_pPlc->DoSomething("READSTRING", &g_PLCAddress.npeiFang, &g_SendMes.cstrnpeiFang);    //配方
			if (g_nAutoReadWorker)
			{
				g_pPlc->DoSomething("READSTRING", &g_PLCAddress.WorkerNum, &g_InfoMes.WorkerSN);  //   员工工号
			}
			double dValue(0);
			CString sTemp = _T("");
			g_pPlc->DoSomething("READDOUBLE64", &g_PLCAddress.SetCutSpeed, &dValue);  //切刀设置速度			
			sTemp.Format(_T("%0.4f"), dValue);
			g_SendMes.nSetCutSpeed = atoi(sTemp);	
			g_pPlc->DoSomething("READDOUBLE64", &g_PLCAddress.RealCutSpeed, &dValue);  //切刀设置速度
			sTemp.Format(_T("%0.4f"), dValue);
			g_SendMes.nRealCutSpeed = atoi(sTemp);
			g_pPlc->DoSomething("READDOUBLE64", &g_PLCAddress.SetTension, &dValue);  //切刀设置速度
			sTemp.Format(_T("%0.4f"), dValue);
			g_SendMes.nSetTension= atoi(sTemp);
			g_pPlc->DoSomething("READDOUBLE64", &g_PLCAddress.RealTension, &dValue);  //切刀设置速度
			sTemp.Format(_T("%0.4f"), dValue);
			g_SendMes.nRealTension= atoi(sTemp);
			g_pPlc->DoSomething("READDOUBLE64", &g_PLCAddress.CutterFrequency, &dValue);  //切刀设置速度
			sTemp.Format(_T("%0.4f"), dValue);
			g_SendMes.nCutterFrequency= atoi(sTemp);;
			g_pPlc->DoSomething("READDOUBLE64", &g_PLCAddress.FoilLife, &dValue);  //切刀设置速度
			sTemp.Format(_T("%0.4f"), dValue);
			g_SendMes.nFoilLife = atoi(sTemp);
			g_pPlc->DoSomething("READDOUBLE64", &g_PLCAddress.AngleLife, &dValue);  //切刀设置速度
			sTemp.Format(_T("%0.4f"), dValue);
			g_SendMes.nAngleLife = atoi(sTemp);
			g_pPlc->DoSomething("READDOUBLE64", &g_PLCAddress.PenYouliang, &dValue);  //切刀设置速度
			sTemp.Format(_T("%0.4f"), dValue);
			g_SendMes.nPenYouliang = atoi(sTemp);

			g_pPlc->DoSomething("READDOUBLE64", &g_PLCAddress.FoilLife1, &dValue);  //切刀设置速度
			sTemp.Format(_T("%0.4f"), dValue);
			g_SendMes.nFoilLife1 = atoi(sTemp);;
			g_pPlc->DoSomething("READDOUBLE64", &g_PLCAddress.AngleLife1, &dValue);  //切刀设置速度
			sTemp.Format(_T("%0.4f"), dValue);
			g_SendMes.nAngleLife1 = atoi(sTemp);;

			g_pPlc->DoSomething("READINT32", &g_PLCAddress.ProductNum, &g_SendMes.nProductNum);  //切刀设置速度
			g_pPlc->DoSomething("READINT32", &g_PLCAddress.PictureFlame, &g_SendMes.nPictureFlame);  //切刀设置速度
			g_pPlc->DoSomething("READINT32", &g_PLCAddress.Result, &g_SendMes.nResult);  //切刀设置速度

			g_SendMes.cstrMaterialNum.TrimRight();
			g_SendMes.cstrnpeiFang.TrimRight();
			
			for (int i = 0; i < 9; i++)
			{
				g_SendMes.SizeAdd.push_back(0);
					
			}
			g_pPlc->DoSomething("READDOUBLE64BATCH", &g_PLCAddress.SizeAdd, &g_SendMes.SizeAdd);
			g_pPlc->DoSomething("READINT32", &g_PLCAddress.nErrorTypeAddress, &g_SendMes.nErrorTypeAddress);  
			g_pPlc->DoSomething("READINT32", &g_PLCAddress.nResultAddress, &g_SendMes.nResultAddress);
		
			for (int i = 0; i < 28; i++)
			{
				g_SendMes.nTotalErrorPerBatchAddress.push_back(0);
			}
			g_pPlc->DoSomething("READFLOAT32BATCH", &g_PLCAddress.nTotalErrorPerBatchAddress, &g_SendMes.nTotalErrorPerBatchAddress);
			RECIPEINFO cstrSendData; //MES数据
			cstrSendData.EquipmentCode = g_InfoMes.EquipmentCode;
			cstrSendData.FactoryCode = g_InfoMes.FactoryCode;
			cstrSendData.TestCode = g_InfoMes.TestCode;
			cstrSendData.WorkerSN = g_InfoMes.WorkerSN;
			cstrSendData.WorkSN = g_InfoMes.WorkSN;
			cstrSendData.IP = g_InfoMes.IP;
			cstrSendData.OutTime = g_InfoMes.OutTime;
			cstrSendData.ProductNum = g_InfoMes.cstrProductNUM;
			cstrSendData.cstrBox1 = g_SendMes.cstrBox1;
			cstrSendData.cstrCreateTime = g_SendMes.cstrCreateTime;
			cstrSendData.cstrEndTime = g_SendMes.cstrEndTime;
			cstrSendData.cstrProductType = g_InfoMes.ProductType;
		
			cstrSendData.nType = 4;
			cstrSendData.nSetCutSpeed = g_SendMes.nSetCutSpeed;
			cstrSendData.nRealCutSpeed = g_SendMes.nRealCutSpeed;
			cstrSendData.nSetTension = g_SendMes.nSetTension;
			cstrSendData.nRealTension = g_SendMes.nRealTension;
			cstrSendData.nCutterFrequency = g_SendMes.nCutterFrequency;
			cstrSendData.nFoilLife = g_SendMes.nFoilLife;
			cstrSendData.nAngleLife = g_SendMes.nAngleLife;
			cstrSendData.nPenYouliang = g_SendMes.nPenYouliang;
			cstrSendData.nProductNum = g_SendMes.nProductNum;
			cstrSendData.nPictureFlame = g_SendMes.nPictureFlame;
			cstrSendData.nResult = g_SendMes.nResult;
			cstrSendData.SizeAdd = g_SendMes.SizeAdd;
			cstrSendData.cstrMaterialNum = g_SendMes.cstrMaterialNum;
			cstrSendData.cstrnpeiFang = g_SendMes.cstrnpeiFang;
			cstrSendData.nErrorTypeAddress = g_SendMes.nErrorTypeAddress;
			cstrSendData.nTotalErrorPerBatchAddress = g_SendMes.nTotalErrorPerBatchAddress;
			cstrSendData.nResultAddress = g_SendMes.nResultAddress;
			cstrSendData.Port = g_InfoMes.Port;
			cstrSendData.IP = g_InfoMes.IP;
			g_SendMes.SizeAdd.clear();
			g_SendMes.nTotalErrorPerBatchAddress.clear();
			((CMesTestDlg*)g_pMainDialog)->SendMES_RAW_MATERIAL_INPUT_FIRST(cstrSendData);
			cstrSendData.nFoilLife = g_SendMes.nFoilLife1;
			cstrSendData.nAngleLife = g_SendMes.nAngleLife1;
			((CMesTestDlg*)g_pMainDialog)->SendMES_RAW_MATERIAL_INPUT_FIRST(cstrSendData);
			Sleep(10000);
		}
		else
		{
			Sleep(2000);
		}
	}
}
void ThreadEx::ThreadHeart(PVOID pVoid)  //发送心跳
{
	Sleep(2000);
	while (true)
	{

		if (g_nSendMes)
		{
			RECIPEINFO cstrSendData; //MES数据
			cstrSendData.Port = g_InfoMes.Port;
			cstrSendData.IP = g_InfoMes.IP;
			cstrSendData.nType = 1;
			cstrSendData.EquipmentCode = g_InfoMes.EquipmentCode;
			cstrSendData.FactoryCode = g_InfoMes.FactoryCode;
			cstrSendData.TestCode = g_InfoMes.TestCode;
			((CMesTestDlg*)g_pMainDialog)->SendMES_USER_RIGHT_VERIFY(cstrSendData);
		}
		Sleep(10000);
	}
}

void ThreadEx::ThreadFTP(PVOID pVoid)
{
	Sleep(2000);
	while (true)
	{
		if (g_nSendMes)
		{

		}
	}
}